<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="">
      <!-- 右侧的菜单 -->
      <el-menu class="el-menu-top" mode="horizontal" background-color="#23262E" text-color="#fff" active-text-color="#409EFF">
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img src="../../assets/logo.png" alt="" class="avatar" />
            <span>个人中心</span>
          </template>
          <el-menu-item index="1-1"><i class="el-icon-s-operation"></i>基本资料</el-menu-item>
          <el-menu-item index="1-2"><i class="el-icon-camera"></i>更换头像</el-menu-item>
          <el-menu-item index="1-3"><i class="el-icon-key"></i>重置密码</el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="logout"><i class="el-icon-switch-button"></i>退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <div class="user-box">
          <!-- 不可以直接写路径 -->
          <img :src="userInfo && userInfo.user_pic ? userInfo.user_pic:tupian" alt="" />
          <span>欢迎 {{userInfo && userInfo.username}}</span>d
        </div>
        <el-menu default-active="/home" class="el-menu-vertical-demo" background-color="#23262E" text-color="#fff" active-text-color="#409EFF" unique-opened>
          <!-- template 标签上不需要加key 不会渲染到页面 -->
          <template v-for="(item,i) in menus">
            <!-- 不包含子菜单的“一级菜单” -->
            <!-- key值绑定到下级标签 -->
            <el-menu-item :index="item.indexPath" v-if="!item.children" :key="i"><i :class="item.icon"></i>{{item.title}}</el-menu-item>
            <!-- 包含子菜单的“一级菜单” -->
            <el-submenu :index="item.indexPath" v-else :key="i">
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{item.title}}</span>
              </template>
              <el-menu-item v-for="(a,b) in item.children" :key="b" :index="a.indexPath">
                <i :class="a.icon"></i>
                {{a.title}}
              </el-menu-item>
            </el-submenu>
            <!-- 包含子菜单的“一级菜单” -->
            <!-- <el-submenu index="3">
              <template slot="title">
                <i class="el-icon-user"></i>
                <span>个人中心</span>
              </template>
              <el-menu-item index="3-1"><i class="el-icon-star-on"></i>基本资料</el-menu-item>
              <el-menu-item index="3-2"><i class="el-icon-star-on"></i>更换头像</el-menu-item>
              <el-menu-item index="3-3"><i class="el-icon-star-on"></i>重置密码</el-menu-item>
            </el-submenu> -->
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapActions, mapState } from 'vuex'
// 导入默认头像
import tupian from '@/assets/logo.png'
export default {
  name: 'Home',
  data () {
    return {
      // 默认头像
      tupian,
      // 菜单的数据
      menus: []
    }
  },
  methods: {
    logout () {
      console.log(1)
      // 提示退出
      this.$confirm('确认要退出吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点击确定触发
        this.$message({
          type: 'success',
          message: '退出成功!'
        })
        // 清除token
        this.$store.commit('updateToken', '')
        // 跳转页面
        this.$router.push('/Login')
      }).catch(() => {
        // 点击取消触发
        this.$message({
          type: 'success',
          message: '取消成功!'
        })
      })
    },
    // 获取菜单数据
    async initMenu () {
      console.log(1)
      const res = await this.$axios.get('/my/menus', {
        // // 发送请求头
        // headers: {
        //   Authorization: this.token
        // }
      })
      console.log(res.data)
      if (res.data.code === 0) {
        this.menus = res.data.data
      }
    },
    ...mapActions(['getUserInfo'])
  },
  computed: {
    ...mapState(['userInfo', 'token'])
  },
  // 数据初始化完成触发
  created () {
    // 判断用户是否已经登录
    if (this.token) {
      this.getUserInfo()
      // 获取菜单数据
      this.initMenu()
    } else {
      this.$router.push('/Login')
    }
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  // 侧边栏菜单的样式
  .el-aside {
    .el-submenu,
    .el-menu-item {
      width: 200px;
      user-select: none;
    }
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}
</style>
